/*
 *  Copyright 2007-2008 by Matthias Buch-Kromann <mbk.isv@cbs.dk>.
 *  *
 *  This file is part of the Open-source Dependency Toolkit (OSDT),
 *  see http://code.google.com/p/open-source-dependency-toolkit.
 *  
 *  The OSDT is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as 
 *  published by the Free Software Foundation, either version 3 of 
 *  the License, or (at your option) any later version.
 * 
 *  The OSDT is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 * 
 *  You should have received a copy of the GNU Lesser General Public 
 *  License along with the OSDT in the files COPYING and COPYING.LESSER.
 *  If not, see http://www.gnu.org/licenses.
 */

package org.osdtsystem.utils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * Provides utility methods used in the OSDT project.
 * @author Matthias Buch-Kromann <mbk.isv@cbs.dk>
 */
public final class Helper {
    /**
     * Sort a list using the standard sort and return the sorted list.
     * This is merely a syntactic short-hand for a method that takes
     * a list as its input and returns the sorted list as the output
     * (Collections.sort returns void). 
     * @param <E> the type of members in the list
     * @param list the list to be sorted
     * @return the sorted list
     */
    public static <E extends Comparable> List<E> sort(List<E> list) {
        Collections.sort(list);
        return list;
    }
    
    /**
     * 
     * @param <O>
     * @param sep
     * @param list
     * @return
     */
    public static <O extends Object> String join(String sep, List<O> list) {
        StringBuilder s = new StringBuilder();
        boolean first = true;
        for (Object o : list) {
            if (first)
                first = false;
            else
                s.append(sep);
            s.append(o);
        }
        return s.toString();        
    }
    
    public static List<Integer> primes(int n) {
        List<Integer> primes = new ArrayList<Integer>();
        boolean[] sieve = new boolean[n+1];
        for (int i = 2; i <= n; ++i)
            sieve[i-2] = true;
        
        // Sieve through primes
        int nsqrt = (int) Math.sqrt(n);
        for (int i = 2; i <= nsqrt; ++i) {
            if (sieve[i]) {
                for (int j = i * 2; j <= n; j += i) {
                    sieve[j] = false;
                }
            }
        }
        
        // Read off primes from sieve
        for (int i = 2; i <= n; ++i) {
            if (sieve[i])
                primes.add(i);
       }
        
       // Return primes
       return Arrays.asList(primes.toArray(new Integer[0]));
    }
    
    
    // Euclid's algorithm for finding the greatest common divisor
    public static int greatestCommonDivisor(int a, int b) {
        return (b == 0) ? a : greatestCommonDivisor(b, a % b);
    }
    
   public static int leastCommonMultiple(int a, int b) {
       return (a / greatestCommonDivisor(a, b)) * b;
    }
}
